//引入express
const { request, response } = require('express');
const express = require('express');

//创建应用对象
const app = express();

//创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的分装
app.get('/server', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');

    //设置响应体
    response.send("HELLO AJAX - 2");

});

//all 可以接收任意类型的请求
app.all('/json-server', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers','*');
    //响应一个数据
    const data = {
        name:'wzw'
    };
    //对对象进行字符串转换
     let str = JSON.stringify(data);
    //设置响应体
    response.send(str);

});
//关于IE缓存问题
app.get('/ie', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');

    //设置响应体
    response.send("HELLO AJAX文智伟真帅122");

});

//延时响应
app.all('/delay', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers','*');
    setTimeout(()=>{
        response.send("延时");
    },3000)
    //设置响应体
});

//jQuery的服务
app.all('/jquery-server', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers','*');
    const data = {
        name:'wzw',
        age:18
    }
    response.send(JSON.stringify(data));
});
//axios服务
app.all('/axios-server', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers','*');
    const data = {
        name:'wzw',
        age:18
    }
    response.send(JSON.stringify(data));
});
//fetch函数
app.all('/fetch-server', (request, response) => {
    //设置响应头 设置允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers','*');
    const data = {
        name:'wzw',
        age:18
    }
    response.send(JSON.stringify(data));
});


//监听端口启动服务
app.listen(8000, ()=>{
    console.log("服务已启动，8000端口监听中...");
});

//jsonp的服务
app.all('/jsonp-server',(request, response)=>{
    // response.send('console.log("jsonp")');
    const data ={
        name: "wzw"
    };
    //将数据转化为字符串
    let str = JSON.stringify(data);
    //返回结果
    response.end(`handle(${str})`);
})

//用户名检测是否存在
app.all('/check-username',(request, response)=>{
    // response.send('console.log("jsonp")');
    const data ={
        exist:1,
        msg:'用户名存在'
    };
    //将数据转化为字符串
    let str = JSON.stringify(data);
    //返回结果
    response.end(`handle(${str})`);
})
//jquery jsonp的传值
app.all('/jquery-jsonp-server',(request, response)=>{
    // response.send('console.log("jsonp")');
    const data ={
        name:'文智伟',
        city:['西安','南昌','北京']
    };
    //将数据转化为字符串
    let str = JSON.stringify(data);
    //接收callback参数
    let cb = request.query.callback;
    //返回结果
    response.end(`${cb}(${str})`);
});


//cors
app.all('/cors-server',(request , response)=>{
    //设置响应头    
    response.setHeader("Access-Control-Allow-Origin","*");
    response.send('hello cors');
})